<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>历史入住记录</title>
        <link rel="icon" th:href="@{/static/favicon.ico}" />
        <link rel="stylesheet" th:href="@{/static/bootstrap/css/bootstrap.min.css}" />
        <link rel="stylesheet" th:href="@{/static/admin/css/main.css}" />
        <script th:src="@{/static/js/vue.min.js}"></script>
        <script th:src="@{/static/js/axios.min.js}"></script>
        <script th:src="@{/static/js/jquery-3.3.1.min.js}"></script>
        <script th:src="@{/static/bootstrap/js/bootstrap.bundle.js}"></script>
        <script th:src="@{/static/admin/js/config.js}"></script>
    </head>

    <body>
        <main>
            <div class="main">
                <div class="search" style="display: flex; flex-direction: column; padding-bottom: 0">
                    <div style="display: flex; margin-bottom: 20px; justify-content: space-between">
                        <button type="button" class="btn btn-primary" onclick="window.history.back()"><i class="iconfont">&#xe66f; </i>返回</button>
                        <form class="form-inline float-left" th:action="@{'/room/checkin/history/'+${id}}" method="get">
                            <div class="form-group">
                                <select name="option">
                                    <option value="user_id" selected>UID</option>
                                    <option value="create_time">预订时间</option>
                                    <option value="checkin_time">入住时间</option>
                                    <option value="checkout_time">退房时间</option>
                                </select>
                                <input style="margin: 0 4px" type="text" name="value" value="" class="form-control" placeholder="输入关键字" />
                            </div>
                            <button type="submit" class="btn btn-primary"><i class="iconfont"> </i>搜索</button>
                        </form>
                    </div>
                    <table class="table" style="text-align: center; margin-bottom: 0">
                        <thead style="color: black">
                            <tr>
                                <th :width="width[0]" scope="col">编号</th>
                                <th :width="width[1]" scope="col">入住客户ID</th>
                                <th :width="width[2]" scope="col">预订时间</th>
                                <th :width="width[3]" scope="col">入住时间</th>
                                <th :width="width[4]" scope="col">退房时间</th>
                                <th :width="width[5]" scope="col">操作</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <div class="table-box" th:style="|display: ${#lists.isEmpty(list) ? 'flex' : 'block'};|">
                    <h1 th:if="${#lists.isEmpty(list)}" class="display-4" style="margin: auto">暂无入住记录</h1>
                    <table class="table" style="text-align: center" th:unless="${#lists.isEmpty(list)}">
                        <tbody>
                            <tr th:each="reservation : ${list}">
                                <th th:text="${reservation.id}">1</th>
                                <td width="106" th:text="${reservation.userId}">3</td>
                                <td th:text="${reservation.createTimeToString()}">2023-07-28 14:20:35</td>
                                <td th:text="${reservation.checkinTimeToString()}">2023-07-28 14:20:35</td>
                                <td th:text="${reservation.checkoutTimeToString() ?: '暂无'}">2023-07-28 14:20:35</td>
                                <td style="padding: 0.4rem">
                                    <button th:id="${reservation.id}" class="delete btn btn-danger">删除</button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div class="submit-box" style="border-top: 1px solid #ccc">
                    <h5 th:text="|共${list.size}条|">共75条</h5>
                </div>
            </div>
        </main>

        <script>
            var vue = new Vue({
                el: ".table",
                data: {
                    width: [77.9688, 169.042, 396.5, 396.5, 396.5, 188.156],
                },
            });

            $(".delete").click(function () {
                if (!confirm("确定要删除吗？")) {
                    return;
                }
                $.ajax({
                    url: "/room/checkin/delete/" + event.target.id,
                    type: "DELETE",
                    data: {},
                    success: function (data) {
                        alert(data.message);
                        if (data.code == 200) {
                            window.location.reload();
                        }
                    },
                    error: function (textStatus, errorThrown) {
                        alert("请求出错：" + textStatus + "，" + errorThrown);
                    },
                });
            });

            $(function () {
                var width = [];
                $("tbody > tr:first th, tbody > tr:first td").each(function () {
                    width.push($(this).innerWidth());
                });
                if (width.length != 0) {
                    vue.width = width;
                }
            });
        </script>
    </body>
</html>
